﻿using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Action = Styx.TreeSharp.Action;

namespace BMSVMM
{
    public partial class Main : CombatRoutine
    {
        
        Composite MMRotation()
        {
            return new PrioritySelector(
                new Decorator(ret => gotTarget && CRSettingsBMSVMM.myPrefs.MisdirectChoice == CRSettingsBMSVMM.Misdirect.Auto&& LastSpell != "Misdirection", CreateMisdirectionBehavior()),
                new Decorator(ret => gotTarget && buffExists(FIRE, Me), AimedShotRotation()),
                new Decorator(ret => gotTarget && DoAoe, CreateMMAoe()),
                Cast("Disengage", ret => gotTarget && Me.CurrentTarget.Distance <= CRSettingsBMSVMM.myPrefs.DisengageDistance && CRSettingsBMSVMM.myPrefs.DisengageDistance != 0 && !spellOnCooldown("Disengage")),
                new Decorator(ret => gotTarget && !debuffExists("Serpent Sting", Me.CurrentTarget) && Me.FocusPercent >= 15 && LastSpell != "Serpent Sting", SSRotation()),
                new Decorator(ret => gotTarget && (!buffExists(STEADY_FOCUS, Me) || (buffExists(STEADY_FOCUS, Me) && buffTimeLeft(STEADY_FOCUS, Me) <= 4000) && Me.CurrentTarget.HealthPercent < 80), SteadyRotation()),
                new Decorator(ret => gotTarget && buffExists(THRILL_OF_THE_HUNT, Me), ThrillRoation()),
                Cast("Silencing Shot", ret => gotTarget && !spellOnCooldown("Silencing Shot")
                     && Me.CurrentTarget.IsCasting
                     && Me.CanInterruptCurrentSpellCast
                     && LastSpell != "Silencing Shot"),
                Cast("Revive Pet", ret => Me.Pet != null && Me.Pet.IsDead),
                Cast("Mend Pet", ret => Me.GotAlivePet
                     && Me.Pet.HealthPercent <= CRSettingsBMSVMM.myPrefs.useMendPet
                     && !buffExists("Mend Pet", Me.Pet)),
                Cast("Concussive Shot", ret => gotTarget && !debuffExists("Concussive Shot", StyxWoW.Me.CurrentTarget) && Me.CurrentTarget.IsPlayer),
                Cast("Concussive Shot", ret => gotTarget && !debuffExists("Concussive Shot", StyxWoW.Me.CurrentTarget) && Me.CurrentTarget.ThreatInfo.RawPercent > 90 && !Me.CurrentTarget.Name.Contains("Dummy")),
                Cast("Concussive Shot", ret => gotTarget && Me.MovementInfo.MovingBackward && !debuffExists("Concussive Shot", Me.CurrentTarget) && LastSpell != "Concussive Shot"),
                DropCastOnMe("Ice Trap", ret => gotTarget
                    && CRSettingsBMSVMM.myPrefs.Kiting
                    && StyxWoW.Me.MovementInfo.MovingBackward && buffExists("Trap Launcher", Me) && !spellOnCooldown("Ice Trap")),
                Cast("Kill Shot", ret => gotTarget && Me.CurrentTarget.HealthPercent <= 20 && !spellOnCooldown("Kill Shot")),
                Cast("Stampede", ret => gotTarget
                    && (!BurstKeySettings.myPrefs.useBurstMode || (BurstKeySettings.myPrefs.useBurstMode && !BurstKeySettings.myPrefs.useBurstStampede))
                    && isBoss
                    && !spellOnCooldown("Stampede")),
                Cast("Rapid Fire", ret => gotTarget
                    && (!BurstKeySettings.myPrefs.useBurstMode || (BurstKeySettings.myPrefs.useBurstRapidFire && !BurstKeySettings.myPrefs.useBurstRapidFire))
                    && isBoss
                    && !spellOnCooldown("Rapid Fire")
                    && LastSpell != "Rapid Fire"),
                Cast("Lifeblood", ret => gotTarget && isBoss && !spellOnCooldown("Lifeblood")),
                Cast("Blood Fury", ret => gotTarget
                    && CRSettingsBMSVMM.myPrefs.useBloodFury
                    && (!BurstKeySettings.myPrefs.useBurstMode || (BurstKeySettings.myPrefs.useBurstMode && !BurstKeySettings.myPrefs.useBurstBloodFury))
                    && isBoss
                    && !spellOnCooldown("Blood Fury")),
                Cast("Berserking", ret => gotTarget
                    && CRSettingsBMSVMM.myPrefs.useBerserking
                    && (!BurstKeySettings.myPrefs.useBurstMode || (BurstKeySettings.myPrefs.useBurstMode && !BurstKeySettings.myPrefs.useBurstBloodFury))
                    && isBoss
                    && !spellOnCooldown("Berserking")),

                //Burst Mode
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && SpellManager.HasSpell(RAPID_FIRE)
                    && !spellOnCooldown(RAPID_FIRE)
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstRapidFire
                    && nextBurstSpellAllowed <= DateTime.Now, CastBurstRapidFire()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && SpellManager.HasSpell(STAMPEDE)
                    && !spellOnCooldown(STAMPEDE)
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstStampede
                    && nextBurstSpellAllowed <= DateTime.Now, CastBurstStampede()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstCrows
                    && !spellOnCooldown(MURDER_OF_CROWS)
                    && nextBurstSpellAllowed <= DateTime.Now, CastBurstMurderOfCrows()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstBerserking
                    && !spellOnCooldown(BERSERKING)
                    && NotHaveHasteBuffs()
                    && nextBurstSpellAllowed <= DateTime.Now, CastBurstBerserking()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstBloodFury
                    && !spellOnCooldown(BLOOD_FURY)
                    && nextBurstSpellAllowed <= DateTime.Now, CastBurstBloodFury()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstTrinket1
                    && CRSettingsBMSVMM.myPrefs.Trinket1Use != CRSettingsBMSVMM.TrinketsUse.Manual
                    && nextTrinketUseAllowed <= DateTime.Now, Trinket1()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstTrinket2
                    && CRSettingsBMSVMM.myPrefs.Trinket2Use != CRSettingsBMSVMM.TrinketsUse.Manual
                    && nextTrinketUseAllowed <= DateTime.Now, Trinket2()),
                new Decorator(ret => gotTarget
                    && BurstEnabled
                    && BurstKeySettings.myPrefs.useBurstMode
                    && BurstKeySettings.myPrefs.useBurstEngiGloves
                    && CRSettingsBMSVMM.myPrefs.GlovesChoice != CRSettingsBMSVMM.useEngiGloves.Manual
                    && nextEngiGlovesUseAllowed <= DateTime.Now, EngineerGloves()),
                //end burstmode

                Cast("Aimed Shot", ret => gotTarget && Me.CurrentTarget.HealthPercent >= 80 && !Me.CurrentTarget.Name.Contains("Dummy")),
                CreateTalent60Behavior(),
                CreateTalent75Behavior(),
                CreateTalent90Behavior(),

                new Decorator(ret => gotTarget 
                    && CRSettingsBMSVMM.myPrefs.useVirmenPotions 
                    && !Me.CurrentTarget.Name.Contains("Dummy") 
                    && isBoss, VirmenPotion()),
                ExploTrapSingle(),
                Cast("Chimera Shot", ret => gotTarget && !spellOnCooldown("Chimera Shot") && Me.FocusPercent >= 45),
                new Decorator(ret => gotTarget && IsWoWBoss(Me.CurrentTarget) && Me.CurrentTarget.InLineOfSight, SnakeTrap()),
                Cast("Arcane Shot", ret => gotTarget && Me.FocusPercent > CRSettingsBMSVMM.myPrefs.useArcaneFocus),
                Cast("Steady Shot", ret => gotTarget && Me.FocusPercent <= 45)
                    );
        }
        Composite CreateMMAoe()
        {
            return new PrioritySelector(
                new Decorator(ret => gotTarget && CRSettingsBMSVMM.myPrefs.MisdirectChoice == CRSettingsBMSVMM.Misdirect.Auto && LastSpell != "Misdirection", CreateMisdirectionBehavior()),
                Cast("Revive Pet", ret => StyxWoW.Me.Pet != null && StyxWoW.Me.Pet.IsDead),
                Cast("Mend Pet", ret => Me.GotAlivePet && Me.Pet.HealthPercent <= CRSettingsBMSVMM.myPrefs.useMendPet && !buffExists("Mend Pet", Me.Pet)),
                ExploTrapMulti(),
                new Decorator(ret => gotTarget && buffExists(THRILL_OF_THE_HUNT, Me), ThrillAoeRotation()),
                CreateMultiShotBehavior(),
                CreateTalent90Behavior(),
                SnakeTrap(),
                Cast("Steady Shot", ret => gotTarget && StyxWoW.Me.FocusPercent < 40)
                );
        }
        Composite SteadyRotation()
        {
            return new PrioritySelector(
                Cast("Steady Shot", ret => gotTarget)
                );
        }
        Composite SSRotation()
        {
            return new PrioritySelector(
                Cast("Serpent Sting", ret => gotTarget)
                );
        }
        Composite AimedShotRotation()
        {
            return new PrioritySelector(
                Cast("Aimed Shot", ret => gotTarget)
                );
        }
    }
}
